package com.qm.struts.admin;

import java.util.List;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.qm.hibernate.TpostDAO;
import com.qm.hibernate.TuserDAO;

public class AdminIndexAction implements Action{

	private Integer id;
	private String type;
	private String method;
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}
	public String delUser()
	{
		TuserDAO ud = new TuserDAO();		
		ud.delUser(getId());
		return "allUsers";
	}
	public String delPost()
	{
		TpostDAO ud = new TpostDAO();		
		ud.delPost(getId());
		return SUCCESS;
	}

	@Override
	public String execute() throws Exception {
		System.out.println("method" +getMethod());
		if ("delUser".equals(getMethod()))
			return delUser();
		if ("delPost".equals(getMethod()))
			return delPost();
		
		TuserDAO ud = new TuserDAO();
		TpostDAO pd = new TpostDAO();
		
		List l = ud.findAll();
		List l2 = pd.findAll();
		
		ActionContext ctx = ActionContext.getContext();
		ctx.getSession().put("allUsers", l);
		ctx.getSession().put("allPosts", l2);
		if("users".equals(getType()))
		return "allUsers";
		return SUCCESS;
	}

}